package com.grablist.feihu.grablist.ui.actviity.user;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.grablist.feihu.grablist.R;
import com.grablist.feihu.grablist.base.BaseActivity;
import com.grablist.feihu.grablist.event.NetErrorEvent;
import com.grablist.feihu.grablist.ui.actviity.user.biz.ApplyCustomBiz;
import com.grablist.feihu.grablist.ui.actviity.user.component.DaggerApplyCustomComponent;
import com.grablist.feihu.grablist.ui.actviity.user.contract.ApplyCustomContract;
import com.grablist.feihu.grablist.ui.actviity.user.module.ApplyCustomModule;
import com.grablist.feihu.grablist.ui.actviity.user.presenter.ApplyCustomPresenter;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import rx.functions.Action1;

public class ApplyCustomAct extends BaseActivity implements ApplyCustomContract.View {

    @Inject
    ApplyCustomPresenter mPresenter;
    @Inject
    ApplyCustomBiz mBiz;

    @Bind(R.id.tv_cancel)
    TextView mTvCancel;
    @Bind(R.id.tv_commit)
    TextView mTvCommit;
    @Bind(R.id.edit_count)
    EditText mEditCount;
    @Bind(R.id.tv_tel)
    TextView mTvTel;

    @Override
    protected void setRoot() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_apply_custom);
    }

    @Override
    protected void setup() {
        DaggerApplyCustomComponent.builder().applyCustomModule(new ApplyCustomModule(this)).build().inject(this);
        mPresenter.setBiz(mBiz);
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        RxView.clicks(mTvCancel)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finishAct();
                    }
                });

        RxView.clicks(mTvCommit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.applyCommit(mEditCount.getText().toString().trim());
                    }
                });

        RxView.clicks(mTvTel)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        mPresenter.telPhone("400-0505-811");
                    }
                });
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBiz.onDestory();
    }
}
